import { eventCustom } from "./EventCustom";


const { ccclass, menu, disallowMultiple, property } = cc._decorator;

@ccclass
/**
 * AdsorptionEffect 吸附效果
 * 
 */
@menu("自定义组件/ScrollView/吸附效果")
@disallowMultiple

export default class ScrollViewAdsorptionEffect extends cc.Component {

    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    @property({
        type: cc.Node,
        tooltip: "吸附点的位置,默认在scrollView中间,和scrollview同层级"
    })
    pn_ControlNode: cc.Node = null;
    // @property({
    //     type: cc.Vec2,
    //     tooltip: "吸附点的位置,默认在scrollView中间"
    // })
    // pn_ControlPoint: cc.Vec2 = cc.v2();


    content: cc.Node = null;


    @property(cc.Layout)
    _layout: cc.Layout = null;
    _oldY
    _touchUp
    _controlPoint
    _OnAdsorption = false;
    _InCheck = false;
    scrollViewNode
    _CumulativeDt = 0;
    _CheckThreshold = 0.2;
    onLoad() {
        this.content = this.scrollview.content;
        this.scrollViewNode = this.scrollview.node
        this._layout = this.content.getComponent(cc.Layout)
        if (!this.pn_ControlNode) {
            this._controlPoint = cc.v2()
        } else {
            this._controlPoint = this.pn_ControlNode.position
        }
    }
    onEnable() {
        this._onScrollEvent();
    }
    onDisable() {
        this._offScrollEvent()
    }
    onDestroy() {
    }

    /** 注册滑动监听 */
    _onScrollEvent() {
        this.scrollViewNode.on(cc.Node.EventType.TOUCH_END, this._TouchUp, this)
        this.scrollViewNode.on(cc.Node.EventType.TOUCH_CANCEL, this._TouchUp, this)
        this.scrollViewNode.on('touch-up', this._TouchUp, this)
        this.scrollViewNode.on('scroll-began', this._ScrollBegan, this)
        this.scrollViewNode.on('scrolling', this._Scrolling, this)
    }
    /**取消滑动监听 */
    _offScrollEvent() {
        if (!this.scrollViewNode) return;
        this.scrollViewNode.off(cc.Node.EventType.TOUCH_END, this._TouchUp, this)
        this.scrollViewNode.off(cc.Node.EventType.TOUCH_CANCEL, this._TouchUp, this)
        this.scrollViewNode.off('touch-up', this._TouchUp, this)
        this.scrollViewNode.off('scrolling', this._Scrolling, this)
        this.scrollViewNode.off('scroll-began', this._ScrollBegan, this)
    }
    _Scrolling(event) {
        // this._touchUp = true;
        this._InCheck = true;
    }
    _TouchUp(event) {
        this._touchUp = true;
        // this._InCheck = true;
    }
    _ScrollBegan(event) {
        this._touchUp = false;
        this._OnAdsorption = false;
        this._InCheck = false;
    }
    check() {
        let parentCenter = this.scrollview.node.convertToWorldSpaceAR(this._controlPoint)
        let nodeCenter = this.content.convertToNodeSpaceAR(parentCenter)
        //查找包含吸引点的节点
        let len = this.content.children.length;
        for (let i = 0; i < len; i++) {
            let item = this.content.children[i];
            if (item) {
                if (item.getBoundingBox().contains(nodeCenter)) {
                    // let string = item.getComponent(item.name).lbl_index.string
                    //Mark:发送数据
                    eventCustom.emit("SV_SelectItem", item)
                    let targetPos = Math.abs(item.y) - (this.scrollview.node.height * 0.5 - this._controlPoint.y)// - this._layout.paddingTop / 2 - this._layout.paddingBottom - this._layout.spacingY / 2
                    if (Math.abs(targetPos - this.content.y) <= 1 || Math.abs(targetPos + this.content.y) <= 1) {
                        // this._touchUp = false;
                        this._InCheck = false;

                        this.scrollview.stopAutoScroll();
                        this._OnAdsorption = false

                    } else {
                        this.scrollview.scrollToOffset(cc.v2(nodeCenter.x, targetPos), 0.5) //向吸引点滑动
                        this._OnAdsorption = true
                    }
                    break;
                }
            }
        }
    }
    _check() {
        // cc.log("this._check;")
        if (Math.abs(this._oldY - this.content.y) <= 0.3) {

            this.check();

        }
    }

    update(dt) {
        if (this._touchUp && this._InCheck && !this._OnAdsorption) {
            this._CumulativeDt += dt;
            if (this._CumulativeDt > this._CheckThreshold) {
                this._check()
                this._oldY = this.content.y
            }
        }
    }
}
